package com.lutzyt.delivery.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lutzyt.delivery.bean.Menu;
import com.lutzyt.delivery.bean.vo.MenuTree;
import com.lutzyt.delivery.service.MenuService;
import com.lutzyt.delivery.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.nodes.Node;

import java.util.List;
import java.util.TreeSet;

/**
 * controller 控制器
 *
 * @author zyt
 * @date 2021-05-16
 */
@Slf4j
@RestController
@RequestMapping("/api/menu")
public class MenuController extends ActionController<MenuService> {
  private MenuService service;

  @GetMapping("/{id}")
  public Result<Menu> getById(@PathVariable Integer id) {
    final Menu bean = service.getById(id);
    return Result.success(bean);
  }

  /**
   * 父菜单列表
   * @return 只含有目录的父菜单列表
   */
  @GetMapping("/parent")
  public Result<List<Menu>> parentList() {
    final List<Menu> list = service.parentList();
    return Result.success(list);
  }

  /**
   * 完整的菜单树
   * @return 菜单树
   */
  @GetMapping("/tree")
  public Result<List<MenuTree>> menuTree() {
    final List<MenuTree> menuTreeList = service.menuTree();
    return Result.success(menuTreeList);
  }


  @PutMapping("/{id}")
  public Result<Void> update(@RequestBody Menu bean) {
    log.info("更新菜单:{}", bean);
    service.update(bean);
    return Result.success();
  }

  @PostMapping("/page")
  public Result<Page<Menu>> page(Integer pageSize, Integer pageNum) {
    final Page<Menu> page = service.findByPage(pageNum, pageSize);
    return Result.success(page);
  }

  @DeleteMapping("/{id}")
  public Result<Void> del(@PathVariable Integer id) {
    log.info("删除菜单:{}", id);
    service.deleteById(id);
    return Result.success();
  }

  @DeleteMapping
  public Result<Void> del(@RequestParam("id") List<String> ids) {
    log.info("删除菜单:{}", ids);
    service.deleteByIds(ids);
    return Result.success();
  }

  @PostMapping
  public Result<Void> insert(@RequestBody Menu bean) {
    log.info("添加菜单:{}", bean);
    service.insert(bean);
    return Result.success();
  }

  @Autowired
  public void setService(MenuService service) {
    this.service = service;
  }
}